﻿using Framework.Resource;
using UnityEngine;
using System.IO;
using System;

/// <summary>
/// 资源帮助类
/// </summary>
/// 
namespace BridgeShips {
    public class AssetHelper : IAssetHelper {
        private AssetBundleManifest m_ResManifest = null;

        public AssetHelper() {
            if (File.Exists( AssetPath.ManifestPath )) {
                AssetBundle manifestAssetBundle = AssetBundle.LoadFromFile( AssetPath.ManifestPath );
                if (manifestAssetBundle != null)
                    m_ResManifest = (AssetBundleManifest)manifestAssetBundle.LoadAsset( "AssetBundleManifest" );
            }
        }
        public object LoadAssetFromPackage( object package, string assetName ) {
            AssetBundle bundle = (AssetBundle)package;
            return bundle.LoadAsset( assetName );
        }
        public T LoadAssetFromPackage<T>( object package, string assetName ) {
            AssetBundle bundle = (AssetBundle)package;
            object obj = bundle.LoadAsset( assetName, typeof( T ) );
            return (T)obj;
        }
        public object InstantiateGameObject( object obj ) {
            return GameObject.Instantiate( (UnityEngine.Object)obj );
        }
        public void DestroyGameObject( object obj ) {
            GameObject.Destroy( (UnityEngine.Object)obj );
        }
        public string[] LoadDepends( string packageName ) {
            return m_ResManifest.GetAllDependencies( packageName );
        }
        public object LoadPackage( string packageName ) {
            return AssetBundle.LoadFromFile( string.Format( "{0}/{1}", AssetPath.AssetBundlePath, packageName ) );
        }
        public void ReleaseAssetPackage( object package ) {
            AssetBundle bundle = (AssetBundle)package;
            bundle.Unload( true );
        }
        public void Destroy() {

        }
    }
}